package com.netsdk.lht2.demo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.netsdk.demo.util.sql.Trajectory;
import com.netsdk.lht2.util.TrajectoryUtil;

import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Demo2 {
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private final static int offset = 0;
    public static void main(String[] args) throws Exception {
        LocalDateTime startTime = LocalDateTime.now();
        Socket    socket = new Socket("127.0.0.1",8091);
        while (true){
            if(socket.isConnected()){
                List<Trajectory[]> data1 = dcx(startTime);
                List<Trajectory[]> data2 = xcd(startTime);
                List<Trajectory[]> data3 = bcn(startTime);
                List<Trajectory[]> data4 = ncb(startTime);
                // 排序
                List<Trajectory> data =new ArrayList<>();
                data1.forEach(el-> data.addAll(Arrays.asList(el)));
                data2.forEach(el-> data.addAll(Arrays.asList(el)));
                data3.forEach(el-> data.addAll(Arrays.asList(el)));
                data4.forEach(el-> data.addAll(Arrays.asList(el)));
                data.sort(Comparator.comparing(Trajectory::getTime));
                StringBuilder sb = new StringBuilder();
                for (Trajectory trajectory : data) {
                    String body = TrajectoryUtil.toPushString(trajectory);
                    sb.append(body);
                }
                System.out.println(new Date() + sb.toString());
                OutputStream outputStream = socket.getOutputStream();
                sb.append("\n");
                String dataStr = sb.toString();
                outputStream.write(dataStr.getBytes(StandardCharsets.UTF_8));
                System.out.println("发送成功："+ new Date());
                Thread.sleep(120*1000);
                startTime = startTime.plusSeconds(150);
            }
        }
    }

    public static List<Trajectory[]> dcx(LocalDateTime startTime){
        String nChannel = "330";
        // 直行 1     2:02 1
        List<Trajectory[]> data1 = TrajectoryUtil.create("2024-12-10 20:00:00",1,nChannel,"1");
        TrajectoryUtil.setTime(data1.get(0), startTime.plusSeconds(122-offset));
        setCarInfo(data1.get(0),10);
//        右转
//        直行 2 1:38 1, 2:12 1
        List<Trajectory[]> data2 = TrajectoryUtil.create("2024-12-10 20:00:00",2,nChannel,"2");
        TrajectoryUtil.setTime(data2.get(0), startTime.plusSeconds(98-offset));
        setCarInfo(data2.get(0),10);
        TrajectoryUtil.setTime(data2.get(1), startTime.plusSeconds(132-offset));
        setCarInfo(data2.get(1),10);
//        左转 1- 1:43 1,
        List<Trajectory[]> data3 = TrajectoryUtil.create("2024-12-10 20:00:00",1,nChannel,"1-");
        TrajectoryUtil.setTime(data3.get(0), startTime.plusSeconds(103-offset));
        setCarInfo(data3.get(0),10);

        List<Trajectory[]> data = new ArrayList<>();
        data.addAll(data1);
        data.addAll(data2);
        data.addAll(data3);
        return data;
    }

    public static List<Trajectory[]> xcd(LocalDateTime startTime){
//        西侧东  324
        String nChannel = "324";
        // 右转 4 2:15 1,
        List<Trajectory[]> data1 = TrajectoryUtil.create("2024-12-10 20:00:00",1,nChannel,"4");
        TrajectoryUtil.setTime(data1.get(0), startTime.plusSeconds(135-offset));
        setCarInfo(data1.get(0),81);

//        直行 3 1:38 1 ,1:51 1, 2:08 1,
        List<Trajectory[]> data2 = TrajectoryUtil.create("2024-12-10 20:00:00",3,nChannel,"3");
        TrajectoryUtil.setTime(data2.get(0), startTime.plusSeconds(98-offset));
        setCarInfo(data2.get(0),10);
        TrajectoryUtil.setTime(data2.get(1), startTime.plusSeconds(111-offset));
        setCarInfo(data2.get(1),91);
        TrajectoryUtil.setTime(data2.get(2), startTime.plusSeconds(128-offset));
        setCarInfo(data2.get(2),10);
//        左转 1  1:38 2
        List<Trajectory[]> data3 = TrajectoryUtil.create("2024-12-10 20:00:00",2,nChannel,"2");
        TrajectoryUtil.setTime(data3.get(0), startTime.plusSeconds(98-offset));
        setCarInfo(data3.get(0),10);
        TrajectoryUtil.setTime(data3.get(1), startTime.plusSeconds(98-offset).plusSeconds(1));
        setCarInfo(data3.get(1),91);
        List<Trajectory[]> data = new ArrayList<>();
        data.addAll(data1);
        data.addAll(data2);
        data.addAll(data3);
        return data;
    }

    public static List<Trajectory[]> bcn(LocalDateTime startTime){
//        北侧2-南  352
        String nChannel = "352";
//        右转 1  00:16 1, 00:55 1, 01:26 1
        List<Trajectory[]> data1 = TrajectoryUtil.create("2024-12-10 20:00:00",3,nChannel,"1");
        TrajectoryUtil.setTime(data1.get(0), startTime.plusSeconds(16));
        setCarInfo(data1.get(0),10); // 私家车
        TrajectoryUtil.setTime(data1.get(1), startTime.plusSeconds(55));
        setCarInfo(data1.get(1),10); // 私家车
        TrajectoryUtil.setTime(data1.get(2), startTime.plusSeconds(86));
        setCarInfo(data1.get(2),10); // 私家车

//        左转 2  01:33 1,
        List<Trajectory[]> data2 = TrajectoryUtil.create("2024-12-10 20:00:00",1,nChannel,"2");
        TrajectoryUtil.setTime(data2.get(0), startTime.plusSeconds(93));
        setCarInfo(data2.get(0),10); // 私家车

//        直行 3  00:00 1 , 00:10 7,
        List<Trajectory[]> data3 = TrajectoryUtil.create("2024-12-10 20:00:00",8,nChannel,"3");
        TrajectoryUtil.setTime(data3.get(0), startTime.plusSeconds(0));
        setCarInfo(data3.get(0),10); // 私家车

        for (int i = 1; i < 8; i++) {
            TrajectoryUtil.setTime(data3.get(i), startTime.plusSeconds(10-offset).plusSeconds(i-1));
        }

//        直行 4  00:00 1 , 00:08 6,
        List<Trajectory[]> data4 = TrajectoryUtil.create("2024-12-10 20:00:00",7,nChannel,"4");
        TrajectoryUtil.setTime(data4.get(0), startTime.plusSeconds(0));
        setCarInfo(data4.get(0),10); // 私家车

        for (int i = 1; i < 7; i++) {
            TrajectoryUtil.setTime(data4.get(i), startTime.plusSeconds(8-offset).plusSeconds(i-1));

        }

//        左转 5  01:00 2, 01:22 1 ,01:28 1,
        List<Trajectory[]> data5 = TrajectoryUtil.create("2024-12-10 20:00:00",4,nChannel,"5");
        TrajectoryUtil.setTime(data5.get(0), startTime.plusSeconds(60));
        setCarInfo(data5.get(0),10); // 私家车
        TrajectoryUtil.setTime(data5.get(1), startTime.plusSeconds(60).plusSeconds(1));
        setCarInfo(data5.get(1),10); // 私家车
        TrajectoryUtil.setTime(data5.get(2), startTime.plusSeconds(82));
        setCarInfo(data5.get(2),10); // 私家车
        TrajectoryUtil.setTime(data5.get(3), startTime.plusSeconds(88));
        setCarInfo(data5.get(3),10); // 私家车


        List<Trajectory[]> data = new ArrayList<>();
        data.addAll(data1);
        data.addAll(data2);
        data.addAll(data3);
        data.addAll(data4);
        data.addAll(data5);
        return data;
    }

    public static List<Trajectory[]> ncb(LocalDateTime startTime){
//        南侧-北  323
        String nChannel = "323";
//        右转 4-  00:00 6
        List<Trajectory[]> data1 = TrajectoryUtil.create("2024-12-10 20:00:00",6,nChannel,"4-");
        for (int i = 0; i < data1.size(); i++) {
            TrajectoryUtil.setTime(data1.get(i), startTime.plusSeconds(2).plusSeconds(i));
        }
//        直行
//        直行 3  00:00 8
        List<Trajectory[]> data2 = TrajectoryUtil.create("2024-12-10 20:00:00",8,nChannel,"3");
        for (int i = 0; i < data2.size(); i++) {
            TrajectoryUtil.setTime(data2.get(i), startTime.plusSeconds(2).plusSeconds(i));
        }
//        直行 2  00:00 7
        List<Trajectory[]> data3 = TrajectoryUtil.create("2024-12-10 20:00:00",7,nChannel,"2");
        for (int i = 0; i < data3.size(); i++) {
            TrajectoryUtil.setTime(data3.get(i), startTime.plusSeconds(2).plusSeconds(i));
        }
//        左转 1  01:05 4
        List<Trajectory[]> data4 = TrajectoryUtil.create("2024-12-10 20:00:00",4,nChannel,"1");
        TrajectoryUtil.setTime(data4.get(0), startTime.plusSeconds(75-offset).plusSeconds(0));
        setCarInfo(data4.get(0),81); // 公交车
        TrajectoryUtil.setTime(data4.get(1), startTime.plusSeconds(75-offset).plusSeconds(1));
        setCarInfo(data4.get(1),10); //
        TrajectoryUtil.setTime(data4.get(2), startTime.plusSeconds(75-offset).plusSeconds(2));
        setCarInfo(data4.get(2),81); // 公交车
        TrajectoryUtil.setTime(data4.get(3), startTime.plusSeconds(75-offset).plusSeconds(3));
        setCarInfo(data4.get(3),81); // 公交车


        List<Trajectory[]> data = new ArrayList<>();
        data.addAll(data1);
        data.addAll(data2);
        data.addAll(data3);
        data.addAll(data4);
        return data;
    }

    private static void setCarInfo(Trajectory[] trajectorys,int randomValue){
        String[] carInfo = getCarInfo(randomValue);
        for (Trajectory trajectory : trajectorys) {
            trajectory.setShipTypeName(carInfo[0]);
            trajectory.setLength(carInfo[1]);
            trajectory.setWidth(carInfo[2]);
            trajectory.setShipTypeClassName(carInfo[3]);
        }
    }

    private static String[] getCarInfo(int randomValue){
        String[] carInfo = new String[]{"私家车","4", "2","小轿车"};
        if (randomValue>=90) {
            carInfo[0] = "面包车";
            carInfo[1] = "6";
            carInfo[2] = "3";
            carInfo[3] = "中型车";
        } else if (randomValue>80 ) {
            carInfo[0] = "公交车";
            carInfo[1] = "10";
            carInfo[2] = "3.5";
            carInfo[3] = "大型车";
        } else if (randomValue > 50 && randomValue<70) {
            carInfo[0] = "出租车";
            carInfo[1] = "4";
            carInfo[2] = "2";
            carInfo[3] = "小轿车";
        } else if (randomValue > 70) {
            carInfo[0] = "大货车";
            carInfo[1] = "10";
            carInfo[2] = "3.5";
            carInfo[3] = "大型车";
        }
        return carInfo;
    }
}
